<!DOCTYPE html>
<html lang="en-US">
  
<!-- Mirrored from heavy_code_industry.gitee.io/code_heavy_industry/pro002-maven/chapter06/verse02.html by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 10 May 2022 05:10:02 GMT -->
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>代码重工</title>
    <meta name="generator" content="VuePress 1.9.5">
    
    <meta name="description" content="代码重工">
    
    <link rel="preload" href="../../assets/css/0.styles.f7de6701.css" as="style"><link rel="preload" href="../../assets/js/app.972bd0f0.js" as="script"><link rel="preload" href="../../assets/js/4.904e76b5.js" as="script"><link rel="preload" href="../../assets/js/53.eae65ff7.js" as="script"><link rel="prefetch" href="../../assets/js/1.d66e04b2.js"><link rel="prefetch" href="../../assets/js/10.7093d550.js"><link rel="prefetch" href="../../assets/js/100.384331b2.js"><link rel="prefetch" href="../../assets/js/101.582f78e7.js"><link rel="prefetch" href="../../assets/js/102.17f2c0ec.js"><link rel="prefetch" href="../../assets/js/103.a530857d.js"><link rel="prefetch" href="../../assets/js/104.62821ce1.js"><link rel="prefetch" href="../../assets/js/105.11cdcc82.js"><link rel="prefetch" href="../../assets/js/106.d57c1e75.js"><link rel="prefetch" href="../../assets/js/107.7ebeb306.js"><link rel="prefetch" href="../../assets/js/108.c067b47b.js"><link rel="prefetch" href="../../assets/js/109.9495d2ca.js"><link rel="prefetch" href="../../assets/js/11.acc2935e.js"><link rel="prefetch" href="../../assets/js/110.7f58dbd3.js"><link rel="prefetch" href="../../assets/js/111.f78239ff.js"><link rel="prefetch" href="../../assets/js/112.8eb72445.js"><link rel="prefetch" href="../../assets/js/113.3f5aff71.js"><link rel="prefetch" href="../../assets/js/114.9f65cf57.js"><link rel="prefetch" href="../../assets/js/115.dcaea004.js"><link rel="prefetch" href="../../assets/js/116.726c5aac.js"><link rel="prefetch" href="../../assets/js/117.54d6c26a.js"><link rel="prefetch" href="../../assets/js/118.3f4e0bda.js"><link rel="prefetch" href="../../assets/js/119.95ed5dc6.js"><link rel="prefetch" href="../../assets/js/12.a5354f50.js"><link rel="prefetch" href="../../assets/js/120.4fd08fac.js"><link rel="prefetch" href="../../assets/js/121.f4aadbe5.js"><link rel="prefetch" href="../../assets/js/122.d588a1e0.js"><link rel="prefetch" href="../../assets/js/123.781e7192.js"><link rel="prefetch" href="../../assets/js/124.ed312291.js"><link rel="prefetch" href="../../assets/js/125.bceffbb9.js"><link rel="prefetch" href="../../assets/js/126.51bf552f.js"><link rel="prefetch" href="../../assets/js/127.4800687b.js"><link rel="prefetch" href="../../assets/js/128.fcdd6f2a.js"><link rel="prefetch" href="../../assets/js/129.2a25d78a.js"><link rel="prefetch" href="../../assets/js/13.88a0c565.js"><link rel="prefetch" href="../../assets/js/130.dcfe7623.js"><link rel="prefetch" href="../../assets/js/131.c29e7c45.js"><link rel="prefetch" href="../../assets/js/132.9a412a4b.js"><link rel="prefetch" href="../../assets/js/133.615218d5.js"><link rel="prefetch" href="../../assets/js/134.2168ca5a.js"><link rel="prefetch" href="../../assets/js/135.a5e262ff.js"><link rel="prefetch" href="../../assets/js/136.7dba5235.js"><link rel="prefetch" href="../../assets/js/137.445576e7.js"><link rel="prefetch" href="../../assets/js/138.577b3f66.js"><link rel="prefetch" href="../../assets/js/139.b646f1ac.js"><link rel="prefetch" href="../../assets/js/14.36391765.js"><link rel="prefetch" href="../../assets/js/140.52abf81e.js"><link rel="prefetch" href="../../assets/js/141.807c626b.js"><link rel="prefetch" href="../../assets/js/142.adf9e127.js"><link rel="prefetch" href="../../assets/js/143.9773d979.js"><link rel="prefetch" href="../../assets/js/144.5658ae76.js"><link rel="prefetch" href="../../assets/js/145.2d2fb13b.js"><link rel="prefetch" href="../../assets/js/146.0e322019.js"><link rel="prefetch" href="../../assets/js/147.50b34ac1.js"><link rel="prefetch" href="../../assets/js/148.463aab7c.js"><link rel="prefetch" href="../../assets/js/149.9bd7c539.js"><link rel="prefetch" href="../../assets/js/15.4a8e9d28.js"><link rel="prefetch" href="../../assets/js/150.9449ca28.js"><link rel="prefetch" href="../../assets/js/151.1961c6db.js"><link rel="prefetch" href="../../assets/js/152.18709656.js"><link rel="prefetch" href="../../assets/js/153.a4bf78f6.js"><link rel="prefetch" href="../../assets/js/154.6c90b1d5.js"><link rel="prefetch" href="../../assets/js/155.024247e2.js"><link rel="prefetch" href="../../assets/js/156.76281b57.js"><link rel="prefetch" href="../../assets/js/157.a7c02212.js"><link rel="prefetch" href="../../assets/js/158.0d00acef.js"><link rel="prefetch" href="../../assets/js/159.9180c937.js"><link rel="prefetch" href="../../assets/js/16.87793b5d.js"><link rel="prefetch" href="../../assets/js/160.d7a98288.js"><link rel="prefetch" href="../../assets/js/161.389ddf84.js"><link rel="prefetch" href="../../assets/js/162.ab22d7dc.js"><link rel="prefetch" href="../../assets/js/163.087257c6.js"><link rel="prefetch" href="../../assets/js/164.addfc5af.js"><link rel="prefetch" href="../../assets/js/165.f90e6b2a.js"><link rel="prefetch" href="../../assets/js/166.fa06e4b6.js"><link rel="prefetch" href="../../assets/js/167.a00e722b.js"><link rel="prefetch" href="../../assets/js/168.e3855223.js"><link rel="prefetch" href="../../assets/js/169.e0c63a4e.js"><link rel="prefetch" href="../../assets/js/17.dec86310.js"><link rel="prefetch" href="../../assets/js/170.7c5835f4.js"><link rel="prefetch" href="../../assets/js/171.7d9ae110.js"><link rel="prefetch" href="../../assets/js/172.4da5caea.js"><link rel="prefetch" href="../../assets/js/173.f8ac6155.js"><link rel="prefetch" href="../../assets/js/174.3b831765.js"><link rel="prefetch" href="../../assets/js/175.01782f3d.js"><link rel="prefetch" href="../../assets/js/176.6eab6644.js"><link rel="prefetch" href="../../assets/js/177.86339767.js"><link rel="prefetch" href="../../assets/js/178.c437c804.js"><link rel="prefetch" href="../../assets/js/179.927227c9.js"><link rel="prefetch" href="../../assets/js/18.9167733d.js"><link rel="prefetch" href="../../assets/js/180.caa2c660.js"><link rel="prefetch" href="../../assets/js/181.c186b8ac.js"><link rel="prefetch" href="../../assets/js/182.0475844f.js"><link rel="prefetch" href="../../assets/js/183.2c5950c8.js"><link rel="prefetch" href="../../assets/js/184.82b336f6.js"><link rel="prefetch" href="../../assets/js/185.e2d4a46e.js"><link rel="prefetch" href="../../assets/js/186.99a3de8a.js"><link rel="prefetch" href="../../assets/js/187.d557500c.js"><link rel="prefetch" href="../../assets/js/188.e81bd251.js"><link rel="prefetch" href="../../assets/js/189.d07d368a.js"><link rel="prefetch" href="../../assets/js/19.017b4ad0.js"><link rel="prefetch" href="../../assets/js/190.173606ea.js"><link rel="prefetch" href="../../assets/js/191.c5ac0c71.js"><link rel="prefetch" href="../../assets/js/192.127d6d93.js"><link rel="prefetch" href="../../assets/js/193.2f9011c9.js"><link rel="prefetch" href="../../assets/js/194.14f534a3.js"><link rel="prefetch" href="../../assets/js/195.65939036.js"><link rel="prefetch" href="../../assets/js/196.6e3cf025.js"><link rel="prefetch" href="../../assets/js/197.4d18b1d7.js"><link rel="prefetch" href="../../assets/js/198.c2e521c3.js"><link rel="prefetch" href="../../assets/js/199.d72c63dd.js"><link rel="prefetch" href="../../assets/js/20.6aa686d0.js"><link rel="prefetch" href="../../assets/js/200.56d7dbd3.js"><link rel="prefetch" href="../../assets/js/201.c72a395d.js"><link rel="prefetch" href="../../assets/js/202.cb32f7af.js"><link rel="prefetch" href="../../assets/js/203.285e6b92.js"><link rel="prefetch" href="../../assets/js/204.ead50027.js"><link rel="prefetch" href="../../assets/js/205.190c1baf.js"><link rel="prefetch" href="../../assets/js/206.4dab4ba0.js"><link rel="prefetch" href="../../assets/js/207.f11eee48.js"><link rel="prefetch" href="../../assets/js/208.e2f8ca82.js"><link rel="prefetch" href="../../assets/js/209.e7e74aff.js"><link rel="prefetch" href="../../assets/js/21.f7d66fe8.js"><link rel="prefetch" href="../../assets/js/210.69c4a0ed.js"><link rel="prefetch" href="../../assets/js/211.a1ab5062.js"><link rel="prefetch" href="../../assets/js/212.6d0473bd.js"><link rel="prefetch" href="../../assets/js/213.bd120419.js"><link rel="prefetch" href="../../assets/js/214.c7d4362c.js"><link rel="prefetch" href="../../assets/js/215.8adc34dc.js"><link rel="prefetch" href="../../assets/js/216.abf8ff23.js"><link rel="prefetch" href="../../assets/js/217.10f9fc46.js"><link rel="prefetch" href="../../assets/js/218.52e31fec.js"><link rel="prefetch" href="../../assets/js/219.ea0551af.js"><link rel="prefetch" href="../../assets/js/22.b1de9b18.js"><link rel="prefetch" href="../../assets/js/220.6be520c8.js"><link rel="prefetch" href="../../assets/js/221.c0d93576.js"><link rel="prefetch" href="../../assets/js/222.1bb0eaff.js"><link rel="prefetch" href="../../assets/js/223.8492b065.js"><link rel="prefetch" href="../../assets/js/224.5897434e.js"><link rel="prefetch" href="../../assets/js/225.51902428.js"><link rel="prefetch" href="../../assets/js/226.9da70eee.js"><link rel="prefetch" href="../../assets/js/227.9e113839.js"><link rel="prefetch" href="../../assets/js/228.d689b27d.js"><link rel="prefetch" href="../../assets/js/229.28f74090.js"><link rel="prefetch" href="../../assets/js/23.e3b58d3d.js"><link rel="prefetch" href="../../assets/js/230.465a35a0.js"><link rel="prefetch" href="../../assets/js/231.219aeb63.js"><link rel="prefetch" href="../../assets/js/232.84035c50.js"><link rel="prefetch" href="../../assets/js/233.ef27424d.js"><link rel="prefetch" href="../../assets/js/234.5dcf1349.js"><link rel="prefetch" href="../../assets/js/235.bcc0c537.js"><link rel="prefetch" href="../../assets/js/236.9bb64465.js"><link rel="prefetch" href="../../assets/js/237.f3300514.js"><link rel="prefetch" href="../../assets/js/238.ca7af9ac.js"><link rel="prefetch" href="../../assets/js/239.fdfe2127.js"><link rel="prefetch" href="../../assets/js/24.2e010582.js"><link rel="prefetch" href="../../assets/js/240.8a4488af.js"><link rel="prefetch" href="../../assets/js/241.9987dfab.js"><link rel="prefetch" href="../../assets/js/242.819ed984.js"><link rel="prefetch" href="../../assets/js/243.bf129ecd.js"><link rel="prefetch" href="../../assets/js/244.030b4b49.js"><link rel="prefetch" href="../../assets/js/245.7663f620.js"><link rel="prefetch" href="../../assets/js/246.ed2b2fc0.js"><link rel="prefetch" href="../../assets/js/247.54dd84f1.js"><link rel="prefetch" href="../../assets/js/248.a1c69038.js"><link rel="prefetch" href="../../assets/js/249.47dd7b3b.js"><link rel="prefetch" href="../../assets/js/25.a5f2f737.js"><link rel="prefetch" href="../../assets/js/250.ec8017a5.js"><link rel="prefetch" href="../../assets/js/251.89ab0cf7.js"><link rel="prefetch" href="../../assets/js/252.5ec1eea4.js"><link rel="prefetch" href="../../assets/js/253.31a37280.js"><link rel="prefetch" href="../../assets/js/254.fcfabb55.js"><link rel="prefetch" href="../../assets/js/255.286f4c17.js"><link rel="prefetch" href="../../assets/js/256.ce77595b.js"><link rel="prefetch" href="../../assets/js/257.cb3677c0.js"><link rel="prefetch" href="../../assets/js/258.ca18e0e9.js"><link rel="prefetch" href="../../assets/js/259.65dc8492.js"><link rel="prefetch" href="../../assets/js/26.cce346fc.js"><link rel="prefetch" href="../../assets/js/260.475ddaba.js"><link rel="prefetch" href="../../assets/js/261.643b784a.js"><link rel="prefetch" href="../../assets/js/262.e5092c67.js"><link rel="prefetch" href="../../assets/js/263.5e85d32e.js"><link rel="prefetch" href="../../assets/js/264.6ff4b789.js"><link rel="prefetch" href="../../assets/js/265.fee7416a.js"><link rel="prefetch" href="../../assets/js/266.8d8a5e44.js"><link rel="prefetch" href="../../assets/js/267.8c62a655.js"><link rel="prefetch" href="../../assets/js/268.762073ff.js"><link rel="prefetch" href="../../assets/js/269.ffe69921.js"><link rel="prefetch" href="../../assets/js/27.22790cda.js"><link rel="prefetch" href="../../assets/js/270.d5bf783f.js"><link rel="prefetch" href="../../assets/js/271.83fd2f70.js"><link rel="prefetch" href="../../assets/js/272.6de67fa9.js"><link rel="prefetch" href="../../assets/js/273.628e1b77.js"><link rel="prefetch" href="../../assets/js/274.7f0d82c6.js"><link rel="prefetch" href="../../assets/js/275.d4478098.js"><link rel="prefetch" href="../../assets/js/276.56649e5b.js"><link rel="prefetch" href="../../assets/js/277.05b881f2.js"><link rel="prefetch" href="../../assets/js/278.c079de28.js"><link rel="prefetch" href="../../assets/js/279.58101972.js"><link rel="prefetch" href="../../assets/js/28.ac0ba3fa.js"><link rel="prefetch" href="../../assets/js/280.5e32c770.js"><link rel="prefetch" href="../../assets/js/281.59fb6cf4.js"><link rel="prefetch" href="../../assets/js/282.f22da512.js"><link rel="prefetch" href="../../assets/js/283.db103d63.js"><link rel="prefetch" href="../../assets/js/284.a373c7e0.js"><link rel="prefetch" href="../../assets/js/285.55c6f720.js"><link rel="prefetch" href="../../assets/js/286.f924c38c.js"><link rel="prefetch" href="../../assets/js/287.52a35525.js"><link rel="prefetch" href="../../assets/js/288.dcc32270.js"><link rel="prefetch" href="../../assets/js/289.835ad2b5.js"><link rel="prefetch" href="../../assets/js/29.6e1dc238.js"><link rel="prefetch" href="../../assets/js/290.749e38a9.js"><link rel="prefetch" href="../../assets/js/291.90916822.js"><link rel="prefetch" href="../../assets/js/292.b453b275.js"><link rel="prefetch" href="../../assets/js/293.11ad0754.js"><link rel="prefetch" href="../../assets/js/294.dddc45b9.js"><link rel="prefetch" href="../../assets/js/295.74ecca38.js"><link rel="prefetch" href="../../assets/js/296.2061efa6.js"><link rel="prefetch" href="../../assets/js/297.b838fcd5.js"><link rel="prefetch" href="../../assets/js/298.8c08be1f.js"><link rel="prefetch" href="../../assets/js/299.5a99490b.js"><link rel="prefetch" href="../../assets/js/3.9ca9fb75.js"><link rel="prefetch" href="../../assets/js/30.64f9cad0.js"><link rel="prefetch" href="../../assets/js/300.a5010762.js"><link rel="prefetch" href="../../assets/js/301.a2f8795a.js"><link rel="prefetch" href="../../assets/js/302.129bdff3.js"><link rel="prefetch" href="../../assets/js/303.57268b2c.js"><link rel="prefetch" href="../../assets/js/304.5556b94f.js"><link rel="prefetch" href="../../assets/js/305.72bbf9b8.js"><link rel="prefetch" href="../../assets/js/306.821163a7.js"><link rel="prefetch" href="../../assets/js/307.b85ceac7.js"><link rel="prefetch" href="../../assets/js/308.7f794216.js"><link rel="prefetch" href="../../assets/js/309.212b7a1d.js"><link rel="prefetch" href="../../assets/js/31.386297cc.js"><link rel="prefetch" href="../../assets/js/310.089b4a08.js"><link rel="prefetch" href="../../assets/js/311.c7f6aabd.js"><link rel="prefetch" href="../../assets/js/312.810271c0.js"><link rel="prefetch" href="../../assets/js/313.030424f0.js"><link rel="prefetch" href="../../assets/js/314.a95451b6.js"><link rel="prefetch" href="../../assets/js/315.f883b88e.js"><link rel="prefetch" href="../../assets/js/316.676ed945.js"><link rel="prefetch" href="../../assets/js/317.0dcbeaef.js"><link rel="prefetch" href="../../assets/js/318.bd172af6.js"><link rel="prefetch" href="../../assets/js/319.e2d01ca1.js"><link rel="prefetch" href="../../assets/js/32.8927ba28.js"><link rel="prefetch" href="../../assets/js/320.09b01482.js"><link rel="prefetch" href="../../assets/js/321.3b70dce2.js"><link rel="prefetch" href="../../assets/js/322.5f7046ad.js"><link rel="prefetch" href="../../assets/js/323.ffd56783.js"><link rel="prefetch" href="../../assets/js/324.cfe0b188.js"><link rel="prefetch" href="../../assets/js/325.cc2f1112.js"><link rel="prefetch" href="../../assets/js/326.635a861b.js"><link rel="prefetch" href="../../assets/js/327.94eacc0e.js"><link rel="prefetch" href="../../assets/js/328.7d4790fb.js"><link rel="prefetch" href="../../assets/js/329.38f7ef56.js"><link rel="prefetch" href="../../assets/js/33.bd1f28e6.js"><link rel="prefetch" href="../../assets/js/330.269edfe2.js"><link rel="prefetch" href="../../assets/js/331.21623a36.js"><link rel="prefetch" href="../../assets/js/332.1f1c659c.js"><link rel="prefetch" href="../../assets/js/333.fe34384d.js"><link rel="prefetch" href="../../assets/js/334.34a514a7.js"><link rel="prefetch" href="../../assets/js/335.9272b0cc.js"><link rel="prefetch" href="../../assets/js/336.796dcaf9.js"><link rel="prefetch" href="../../assets/js/337.bfd29430.js"><link rel="prefetch" href="../../assets/js/338.fdb020fd.js"><link rel="prefetch" href="../../assets/js/339.d7f19890.js"><link rel="prefetch" href="../../assets/js/34.36a2460a.js"><link rel="prefetch" href="../../assets/js/340.b22b3f52.js"><link rel="prefetch" href="../../assets/js/341.47e1d3f4.js"><link rel="prefetch" href="../../assets/js/342.38ca17d1.js"><link rel="prefetch" href="../../assets/js/343.2e7874e5.js"><link rel="prefetch" href="../../assets/js/344.6697aea1.js"><link rel="prefetch" href="../../assets/js/345.ad3be23a.js"><link rel="prefetch" href="../../assets/js/346.d40fa749.js"><link rel="prefetch" href="../../assets/js/347.881934aa.js"><link rel="prefetch" href="../../assets/js/348.2e8d29b9.js"><link rel="prefetch" href="../../assets/js/349.87f51cb4.js"><link rel="prefetch" href="../../assets/js/35.80f36815.js"><link rel="prefetch" href="../../assets/js/350.06081b68.js"><link rel="prefetch" href="../../assets/js/351.f883c9ae.js"><link rel="prefetch" href="../../assets/js/352.8fcd0728.js"><link rel="prefetch" href="../../assets/js/353.2dd787b0.js"><link rel="prefetch" href="../../assets/js/354.689db664.js"><link rel="prefetch" href="../../assets/js/355.b9a083ea.js"><link rel="prefetch" href="../../assets/js/356.3eaa4bf3.js"><link rel="prefetch" href="../../assets/js/357.2f05ac11.js"><link rel="prefetch" href="../../assets/js/358.c6777610.js"><link rel="prefetch" href="../../assets/js/359.2ae0c8ac.js"><link rel="prefetch" href="../../assets/js/36.f9bba134.js"><link rel="prefetch" href="../../assets/js/360.c9811603.js"><link rel="prefetch" href="../../assets/js/361.5c3068d4.js"><link rel="prefetch" href="../../assets/js/362.a80ddcc6.js"><link rel="prefetch" href="../../assets/js/363.ccbb8d6e.js"><link rel="prefetch" href="../../assets/js/364.8da5de40.js"><link rel="prefetch" href="../../assets/js/365.0d9f6c8a.js"><link rel="prefetch" href="../../assets/js/366.e410e582.js"><link rel="prefetch" href="../../assets/js/367.f8689b17.js"><link rel="prefetch" href="../../assets/js/368.57119763.js"><link rel="prefetch" href="../../assets/js/369.f92c7aed.js"><link rel="prefetch" href="../../assets/js/37.8935c848.js"><link rel="prefetch" href="../../assets/js/370.8e896c3a.js"><link rel="prefetch" href="../../assets/js/371.e2565759.js"><link rel="prefetch" href="../../assets/js/372.0028b0ce.js"><link rel="prefetch" href="../../assets/js/373.82bfe997.js"><link rel="prefetch" href="../../assets/js/374.434f814f.js"><link rel="prefetch" href="../../assets/js/375.8478c681.js"><link rel="prefetch" href="../../assets/js/376.998ce2de.js"><link rel="prefetch" href="../../assets/js/377.670e9be5.js"><link rel="prefetch" href="../../assets/js/378.c9cfc01b.js"><link rel="prefetch" href="../../assets/js/379.0aacc719.js"><link rel="prefetch" href="../../assets/js/38.86fba1a7.js"><link rel="prefetch" href="../../assets/js/380.14a91411.js"><link rel="prefetch" href="../../assets/js/381.9e783c93.js"><link rel="prefetch" href="../../assets/js/382.cf2ffc03.js"><link rel="prefetch" href="../../assets/js/383.c32b0740.js"><link rel="prefetch" href="../../assets/js/384.4280025f.js"><link rel="prefetch" href="../../assets/js/385.a816d541.js"><link rel="prefetch" href="../../assets/js/386.1d8fc395.js"><link rel="prefetch" href="../../assets/js/387.bc22626f.js"><link rel="prefetch" href="../../assets/js/388.1cf59352.js"><link rel="prefetch" href="../../assets/js/389.a09795bf.js"><link rel="prefetch" href="../../assets/js/39.c8c0ff8e.js"><link rel="prefetch" href="../../assets/js/390.6a223692.js"><link rel="prefetch" href="../../assets/js/391.733d45f0.js"><link rel="prefetch" href="../../assets/js/392.d76fd3df.js"><link rel="prefetch" href="../../assets/js/393.c885df90.js"><link rel="prefetch" href="../../assets/js/394.c344d389.js"><link rel="prefetch" href="../../assets/js/395.687cbd0a.js"><link rel="prefetch" href="../../assets/js/396.57e0df54.js"><link rel="prefetch" href="../../assets/js/397.1a31ad36.js"><link rel="prefetch" href="../../assets/js/398.59535325.js"><link rel="prefetch" href="../../assets/js/399.0a11557f.js"><link rel="prefetch" href="../../assets/js/40.c0f7e504.js"><link rel="prefetch" href="../../assets/js/400.7b2afabb.js"><link rel="prefetch" href="../../assets/js/401.4b72ea6e.js"><link rel="prefetch" href="../../assets/js/402.b94737bf.js"><link rel="prefetch" href="../../assets/js/403.a838c189.js"><link rel="prefetch" href="../../assets/js/404.9a221abc.js"><link rel="prefetch" href="../../assets/js/405.f8f3b954.js"><link rel="prefetch" href="../../assets/js/406.2de8752c.js"><link rel="prefetch" href="../../assets/js/407.eb35a188.js"><link rel="prefetch" href="../../assets/js/408.47acaedf.js"><link rel="prefetch" href="../../assets/js/409.aa2afaca.js"><link rel="prefetch" href="../../assets/js/41.e7e858c4.js"><link rel="prefetch" href="../../assets/js/410.722e5009.js"><link rel="prefetch" href="../../assets/js/411.2dbe321f.js"><link rel="prefetch" href="../../assets/js/412.39ed2d1e.js"><link rel="prefetch" href="../../assets/js/413.a088f973.js"><link rel="prefetch" href="../../assets/js/414.87f8d239.js"><link rel="prefetch" href="../../assets/js/415.f90a1394.js"><link rel="prefetch" href="../../assets/js/416.ea4ba38d.js"><link rel="prefetch" href="../../assets/js/417.8a1c9676.js"><link rel="prefetch" href="../../assets/js/418.79315748.js"><link rel="prefetch" href="../../assets/js/419.1e3ddf12.js"><link rel="prefetch" href="../../assets/js/42.8202bb44.js"><link rel="prefetch" href="../../assets/js/420.b6d9e0cb.js"><link rel="prefetch" href="../../assets/js/421.0c74ef64.js"><link rel="prefetch" href="../../assets/js/422.94c20620.js"><link rel="prefetch" href="../../assets/js/423.fa0d6946.js"><link rel="prefetch" href="../../assets/js/424.9036ac4f.js"><link rel="prefetch" href="../../assets/js/425.8d8e82da.js"><link rel="prefetch" href="../../assets/js/426.03b7da89.js"><link rel="prefetch" href="../../assets/js/427.e3c8c690.js"><link rel="prefetch" href="../../assets/js/428.96fdfdce.js"><link rel="prefetch" href="../../assets/js/429.a8863be5.js"><link rel="prefetch" href="../../assets/js/43.55e30ded.js"><link rel="prefetch" href="../../assets/js/430.305aa090.js"><link rel="prefetch" href="../../assets/js/431.e02d3725.js"><link rel="prefetch" href="../../assets/js/432.83fa148f.js"><link rel="prefetch" href="../../assets/js/433.1f01715d.js"><link rel="prefetch" href="../../assets/js/434.f3354ec6.js"><link rel="prefetch" href="../../assets/js/435.245644ed.js"><link rel="prefetch" href="../../assets/js/436.3c933a14.js"><link rel="prefetch" href="../../assets/js/437.b62325fb.js"><link rel="prefetch" href="../../assets/js/438.8e122783.js"><link rel="prefetch" href="../../assets/js/439.e95fa7e6.js"><link rel="prefetch" href="../../assets/js/44.e5c90e7a.js"><link rel="prefetch" href="../../assets/js/440.5c168090.js"><link rel="prefetch" href="../../assets/js/441.f8835ac7.js"><link rel="prefetch" href="../../assets/js/442.cf1de5dc.js"><link rel="prefetch" href="../../assets/js/443.50ec8b3c.js"><link rel="prefetch" href="../../assets/js/444.1600e74f.js"><link rel="prefetch" href="../../assets/js/445.795f336d.js"><link rel="prefetch" href="../../assets/js/446.1fb71b0f.js"><link rel="prefetch" href="../../assets/js/447.0136df19.js"><link rel="prefetch" href="../../assets/js/448.abf11aa4.js"><link rel="prefetch" href="../../assets/js/449.4f99bee8.js"><link rel="prefetch" href="../../assets/js/45.89368cde.js"><link rel="prefetch" href="../../assets/js/450.55da381a.js"><link rel="prefetch" href="../../assets/js/451.97a68e8d.js"><link rel="prefetch" href="../../assets/js/452.9dd8c55e.js"><link rel="prefetch" href="../../assets/js/453.3167da0d.js"><link rel="prefetch" href="../../assets/js/454.18fad993.js"><link rel="prefetch" href="../../assets/js/455.24cbebda.js"><link rel="prefetch" href="../../assets/js/456.a2f19b40.js"><link rel="prefetch" href="../../assets/js/457.27f4fb5a.js"><link rel="prefetch" href="../../assets/js/458.29941084.js"><link rel="prefetch" href="../../assets/js/459.b0cc6f89.js"><link rel="prefetch" href="../../assets/js/46.1f553681.js"><link rel="prefetch" href="../../assets/js/460.86ef4965.js"><link rel="prefetch" href="../../assets/js/461.c0b0f695.js"><link rel="prefetch" href="../../assets/js/462.8b86c3a9.js"><link rel="prefetch" href="../../assets/js/463.79aadbb4.js"><link rel="prefetch" href="../../assets/js/464.9735f749.js"><link rel="prefetch" href="../../assets/js/465.4bdede2b.js"><link rel="prefetch" href="../../assets/js/466.ef630311.js"><link rel="prefetch" href="../../assets/js/467.8664e027.js"><link rel="prefetch" href="../../assets/js/468.89b3d4ff.js"><link rel="prefetch" href="../../assets/js/469.74e0af2c.js"><link rel="prefetch" href="../../assets/js/47.5f78034c.js"><link rel="prefetch" href="../../assets/js/470.9643f84e.js"><link rel="prefetch" href="../../assets/js/471.e3a1e16a.js"><link rel="prefetch" href="../../assets/js/472.116791e2.js"><link rel="prefetch" href="../../assets/js/473.3bb6cb3d.js"><link rel="prefetch" href="../../assets/js/474.1a7a0724.js"><link rel="prefetch" href="../../assets/js/475.76c29d71.js"><link rel="prefetch" href="../../assets/js/476.fe1fd93a.js"><link rel="prefetch" href="../../assets/js/477.e3f203de.js"><link rel="prefetch" href="../../assets/js/478.b34f8b1c.js"><link rel="prefetch" href="../../assets/js/479.06d48874.js"><link rel="prefetch" href="../../assets/js/48.0c8a5846.js"><link rel="prefetch" href="../../assets/js/480.ffc77ad4.js"><link rel="prefetch" href="../../assets/js/481.2475d5aa.js"><link rel="prefetch" href="../../assets/js/482.5da370b4.js"><link rel="prefetch" href="../../assets/js/483.2531f6c1.js"><link rel="prefetch" href="../../assets/js/484.3f8b3ccf.js"><link rel="prefetch" href="../../assets/js/485.fb51e0ee.js"><link rel="prefetch" href="../../assets/js/486.881d8443.js"><link rel="prefetch" href="../../assets/js/487.36a959cb.js"><link rel="prefetch" href="../../assets/js/488.086e154c.js"><link rel="prefetch" href="../../assets/js/489.2afd2906.js"><link rel="prefetch" href="../../assets/js/49.40a46041.js"><link rel="prefetch" href="../../assets/js/490.a517239f.js"><link rel="prefetch" href="../../assets/js/491.c1f416dd.js"><link rel="prefetch" href="../../assets/js/492.e36de27b.js"><link rel="prefetch" href="../../assets/js/493.af28d057.js"><link rel="prefetch" href="../../assets/js/494.00509c64.js"><link rel="prefetch" href="../../assets/js/495.8ed86db6.js"><link rel="prefetch" href="../../assets/js/496.412e9f2d.js"><link rel="prefetch" href="../../assets/js/497.ed9f0eeb.js"><link rel="prefetch" href="../../assets/js/498.2112afec.js"><link rel="prefetch" href="../../assets/js/499.743df60a.js"><link rel="prefetch" href="../../assets/js/5.6f1dbbc3.js"><link rel="prefetch" href="../../assets/js/50.54de97a0.js"><link rel="prefetch" href="../../assets/js/500.ab62befa.js"><link rel="prefetch" href="../../assets/js/501.0d54ba9a.js"><link rel="prefetch" href="../../assets/js/502.2f3bf836.js"><link rel="prefetch" href="../../assets/js/503.c3473b0e.js"><link rel="prefetch" href="../../assets/js/504.cb511490.js"><link rel="prefetch" href="../../assets/js/505.a2cc8bdc.js"><link rel="prefetch" href="../../assets/js/506.f8ea663a.js"><link rel="prefetch" href="../../assets/js/507.9cc67e94.js"><link rel="prefetch" href="../../assets/js/508.e08708a1.js"><link rel="prefetch" href="../../assets/js/509.437502c3.js"><link rel="prefetch" href="../../assets/js/51.af5d6a98.js"><link rel="prefetch" href="../../assets/js/510.f5e8a583.js"><link rel="prefetch" href="../../assets/js/511.6fe511a5.js"><link rel="prefetch" href="../../assets/js/512.459d2d68.js"><link rel="prefetch" href="../../assets/js/513.a5f4a768.js"><link rel="prefetch" href="../../assets/js/514.5fd44c68.js"><link rel="prefetch" href="../../assets/js/515.c046ffb4.js"><link rel="prefetch" href="../../assets/js/516.a972f386.js"><link rel="prefetch" href="../../assets/js/517.7c804920.js"><link rel="prefetch" href="../../assets/js/518.ff4a2545.js"><link rel="prefetch" href="../../assets/js/519.b1a8d590.js"><link rel="prefetch" href="../../assets/js/52.b766b60a.js"><link rel="prefetch" href="../../assets/js/520.2d34cada.js"><link rel="prefetch" href="../../assets/js/521.f14247fe.js"><link rel="prefetch" href="../../assets/js/522.38b72172.js"><link rel="prefetch" href="../../assets/js/523.203fa4ac.js"><link rel="prefetch" href="../../assets/js/524.236cda03.js"><link rel="prefetch" href="../../assets/js/525.8caaad18.js"><link rel="prefetch" href="../../assets/js/526.9a217f1e.js"><link rel="prefetch" href="../../assets/js/527.06dc5a4f.js"><link rel="prefetch" href="../../assets/js/528.fa884c79.js"><link rel="prefetch" href="../../assets/js/529.d39c75ca.js"><link rel="prefetch" href="../../assets/js/530.65b18758.js"><link rel="prefetch" href="../../assets/js/531.85af388d.js"><link rel="prefetch" href="../../assets/js/532.9d3f253a.js"><link rel="prefetch" href="../../assets/js/533.77c2fa0b.js"><link rel="prefetch" href="../../assets/js/534.924b0eb2.js"><link rel="prefetch" href="../../assets/js/535.5854a4b0.js"><link rel="prefetch" href="../../assets/js/536.8b57b538.js"><link rel="prefetch" href="../../assets/js/537.ccb7ba0f.js"><link rel="prefetch" href="../../assets/js/538.b9c6c631.js"><link rel="prefetch" href="../../assets/js/539.63164d8a.js"><link rel="prefetch" href="../../assets/js/54.9b89b352.js"><link rel="prefetch" href="../../assets/js/540.7e721167.js"><link rel="prefetch" href="../../assets/js/541.0cd3f048.js"><link rel="prefetch" href="../../assets/js/542.6c5df759.js"><link rel="prefetch" href="../../assets/js/543.e1eebdf2.js"><link rel="prefetch" href="../../assets/js/544.a3042061.js"><link rel="prefetch" href="../../assets/js/545.1a468f89.js"><link rel="prefetch" href="../../assets/js/546.892c1841.js"><link rel="prefetch" href="../../assets/js/547.c6b5ef7e.js"><link rel="prefetch" href="../../assets/js/548.19d884b6.js"><link rel="prefetch" href="../../assets/js/549.7c34a773.js"><link rel="prefetch" href="../../assets/js/55.b8a1964e.js"><link rel="prefetch" href="../../assets/js/550.25f7d121.js"><link rel="prefetch" href="../../assets/js/551.b9f92580.js"><link rel="prefetch" href="../../assets/js/552.bf452147.js"><link rel="prefetch" href="../../assets/js/553.4815ac56.js"><link rel="prefetch" href="../../assets/js/554.c253ebb4.js"><link rel="prefetch" href="../../assets/js/555.4dbef2bf.js"><link rel="prefetch" href="../../assets/js/556.69f1a9f4.js"><link rel="prefetch" href="../../assets/js/557.679e8f40.js"><link rel="prefetch" href="../../assets/js/558.9515c7f8.js"><link rel="prefetch" href="../../assets/js/559.eb17cff7.js"><link rel="prefetch" href="../../assets/js/56.88fa6666.js"><link rel="prefetch" href="../../assets/js/560.0de96a04.js"><link rel="prefetch" href="../../assets/js/561.cdf49465.js"><link rel="prefetch" href="../../assets/js/562.2e895986.js"><link rel="prefetch" href="../../assets/js/563.40a31d36.js"><link rel="prefetch" href="../../assets/js/564.08f15bf2.js"><link rel="prefetch" href="../../assets/js/565.e7625c6a.js"><link rel="prefetch" href="../../assets/js/566.71ee556d.js"><link rel="prefetch" href="../../assets/js/567.7b3e4bc8.js"><link rel="prefetch" href="../../assets/js/568.1a3c07dc.js"><link rel="prefetch" href="../../assets/js/569.4c496338.js"><link rel="prefetch" href="../../assets/js/57.519e80e3.js"><link rel="prefetch" href="../../assets/js/570.d152813e.js"><link rel="prefetch" href="../../assets/js/571.e46946bd.js"><link rel="prefetch" href="../../assets/js/572.ae215a0d.js"><link rel="prefetch" href="../../assets/js/573.da7b2e6c.js"><link rel="prefetch" href="../../assets/js/574.f068c09e.js"><link rel="prefetch" href="../../assets/js/575.f2a128db.js"><link rel="prefetch" href="../../assets/js/576.caf5cd68.js"><link rel="prefetch" href="../../assets/js/577.b6c04041.js"><link rel="prefetch" href="../../assets/js/578.a7707d73.js"><link rel="prefetch" href="../../assets/js/579.ed17d056.js"><link rel="prefetch" href="../../assets/js/58.963c2309.js"><link rel="prefetch" href="../../assets/js/580.88a2ce36.js"><link rel="prefetch" href="../../assets/js/581.f99a65fa.js"><link rel="prefetch" href="../../assets/js/582.220ce66c.js"><link rel="prefetch" href="../../assets/js/583.5d42e8cc.js"><link rel="prefetch" href="../../assets/js/584.0737b3ce.js"><link rel="prefetch" href="../../assets/js/585.516b187d.js"><link rel="prefetch" href="../../assets/js/586.420c42c4.js"><link rel="prefetch" href="../../assets/js/587.e3deb3dd.js"><link rel="prefetch" href="../../assets/js/588.c9bfc1e3.js"><link rel="prefetch" href="../../assets/js/589.ee0f90f3.js"><link rel="prefetch" href="../../assets/js/59.d9b10f83.js"><link rel="prefetch" href="../../assets/js/590.ea859bba.js"><link rel="prefetch" href="../../assets/js/591.4b9781fd.js"><link rel="prefetch" href="../../assets/js/592.c9da8cab.js"><link rel="prefetch" href="../../assets/js/6.064acd80.js"><link rel="prefetch" href="../../assets/js/60.7e89993c.js"><link rel="prefetch" href="../../assets/js/61.f2039f26.js"><link rel="prefetch" href="../../assets/js/62.474d18a7.js"><link rel="prefetch" href="../../assets/js/63.3a64d334.js"><link rel="prefetch" href="../../assets/js/64.4a3787c2.js"><link rel="prefetch" href="../../assets/js/65.120ee13f.js"><link rel="prefetch" href="../../assets/js/66.e7ff207d.js"><link rel="prefetch" href="../../assets/js/67.55a68541.js"><link rel="prefetch" href="../../assets/js/68.33f8c7cd.js"><link rel="prefetch" href="../../assets/js/69.69d65c56.js"><link rel="prefetch" href="../../assets/js/7.ce2273f5.js"><link rel="prefetch" href="../../assets/js/70.93a544a5.js"><link rel="prefetch" href="../../assets/js/71.6eb8d306.js"><link rel="prefetch" href="../../assets/js/72.8cbae6a9.js"><link rel="prefetch" href="../../assets/js/73.c737f071.js"><link rel="prefetch" href="../../assets/js/74.d0ea9fc8.js"><link rel="prefetch" href="../../assets/js/75.2219607f.js"><link rel="prefetch" href="../../assets/js/76.278079e2.js"><link rel="prefetch" href="../../assets/js/77.88eadfbc.js"><link rel="prefetch" href="../../assets/js/78.257c919e.js"><link rel="prefetch" href="../../assets/js/79.e6f5d078.js"><link rel="prefetch" href="../../assets/js/8.e2571ee7.js"><link rel="prefetch" href="../../assets/js/80.40ba7856.js"><link rel="prefetch" href="../../assets/js/81.1fab2c8f.js"><link rel="prefetch" href="../../assets/js/82.6eaad50e.js"><link rel="prefetch" href="../../assets/js/83.9f3e6e81.js"><link rel="prefetch" href="../../assets/js/84.060f32c2.js"><link rel="prefetch" href="../../assets/js/85.c069396c.js"><link rel="prefetch" href="../../assets/js/86.ced213eb.js"><link rel="prefetch" href="../../assets/js/87.763c4879.js"><link rel="prefetch" href="../../assets/js/88.69fe1a6a.js"><link rel="prefetch" href="../../assets/js/89.b87612a8.js"><link rel="prefetch" href="../../assets/js/9.5f2fc60c.js"><link rel="prefetch" href="../../assets/js/90.6132ccb6.js"><link rel="prefetch" href="../../assets/js/91.27dbc3bd.js"><link rel="prefetch" href="../../assets/js/92.88832461.js"><link rel="prefetch" href="../../assets/js/93.0166bd69.js"><link rel="prefetch" href="../../assets/js/94.e29b12f1.js"><link rel="prefetch" href="../../assets/js/95.f1245827.js"><link rel="prefetch" href="../../assets/js/96.9c45438e.js"><link rel="prefetch" href="../../assets/js/97.e8783a06.js"><link rel="prefetch" href="../../assets/js/98.60628afa.js"><link rel="prefetch" href="../../assets/js/99.b3cb9d0f.js">
    <link rel="stylesheet" href="../../assets/css/0.styles.f7de6701.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="../../index.html" class="home-link router-link-active"><!----> <span class="site-name">代码重工</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <!----></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><!---->  <!----> </aside> <main class="page"> <div class="theme-default-content content__default"><p></p><div class="table-of-contents"><ul><li><a href="#_1、数据建模">1、数据建模</a><ul><li><a href="#_1物理建模">①物理建模</a></li><li><a href="#_2逻辑建模">②逻辑建模</a></li></ul></li><li><a href="#_2、数据库连接信息">2、数据库连接信息</a></li><li><a href="#_3、获取数据库连接">3、获取数据库连接</a><ul><li><a href="#_1创建-jdbcutils-工具类">①创建 JDBCUtils 工具类</a></li><li><a href="#_2创建-javax-sql-datasource-对象">②创建 javax.sql.DataSource 对象</a></li><li><a href="#_3创建-threadlocal-对象">③创建 ThreadLocal 对象</a></li><li><a href="#_4声明方法-获取数据库连接">④声明方法：获取数据库连接</a></li><li><a href="#_5声明方法-释放数据库连接">⑤声明方法：释放数据库连接</a></li><li><a href="#_6初步测试">⑥初步测试</a></li><li><a href="#_7完整代码">⑦完整代码</a></li></ul></li><li><a href="#_4、basedao">4、BaseDao</a><ul><li><a href="#_1泛型的说明">①泛型的说明</a></li><li><a href="#_2创建-queryrunner-对象">②创建 QueryRunner 对象</a></li><li><a href="#_3通用增删改方法">③通用增删改方法</a></li><li><a href="#_4查询单个对象">④查询单个对象</a></li><li><a href="#_5查询多个对象">⑤查询多个对象</a></li><li><a href="#_6测试">⑥测试</a></li></ul></li><li><a href="#_5、子类-dao">5、子类 Dao</a></li></ul></div><p></p> <h1 id="第二节-搭建环境-持久化层"><a href="#第二节-搭建环境-持久化层" class="header-anchor">#</a> 第二节 搭建环境：持久化层</h1> <h2 id="_1、数据建模"><a href="#_1、数据建模" class="header-anchor">#</a> 1、数据建模</h2> <h3 id="_1物理建模"><a href="#_1物理建模" class="header-anchor">#</a> ①物理建模</h3> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">create</span> <span class="token keyword">database</span> db_imperial_court<span class="token punctuation">;</span>

<span class="token keyword">use</span> db_imperial_court<span class="token punctuation">;</span>

<span class="token keyword">create</span> <span class="token keyword">table</span> t_emp
<span class="token punctuation">(</span>
    emp_id         <span class="token keyword">int</span> <span class="token keyword">primary</span> <span class="token keyword">key</span> <span class="token keyword">auto_increment</span><span class="token punctuation">,</span>
    emp_name       <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">not</span> <span class="token boolean">null</span><span class="token punctuation">,</span>
    emp_position   <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">not</span> <span class="token boolean">null</span><span class="token punctuation">,</span>
    login_account  <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">not</span> <span class="token boolean">null</span> <span class="token keyword">unique</span><span class="token punctuation">,</span>
    login_password <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">not</span> <span class="token boolean">null</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">insert</span> <span class="token keyword">into</span> t_emp<span class="token punctuation">(</span>emp_name<span class="token punctuation">,</span> emp_position<span class="token punctuation">,</span> login_account<span class="token punctuation">,</span> login_password<span class="token punctuation">)</span>
<span class="token keyword">values</span> <span class="token punctuation">(</span><span class="token string">'爱新觉罗·玄烨'</span><span class="token punctuation">,</span> <span class="token string">'emperor'</span><span class="token punctuation">,</span> <span class="token string">'xiaoxuanzi1654'</span><span class="token punctuation">,</span> <span class="token string">'25325C896624D444B2E241807DCAC98B'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment"># 16540504</span>
       <span class="token punctuation">(</span><span class="token string">'纳兰明珠'</span><span class="token punctuation">,</span> <span class="token string">'minister'</span><span class="token punctuation">,</span> <span class="token string">'brightball1635'</span><span class="token punctuation">,</span> <span class="token string">'A580D0EF93C22036C859E194C14CB777'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>   <span class="token comment"># 16351119</span>
       <span class="token punctuation">(</span><span class="token string">'赫舍里·索额图'</span><span class="token punctuation">,</span> <span class="token string">'minister'</span><span class="token punctuation">,</span> <span class="token string">'tutu1636'</span><span class="token punctuation">,</span> <span class="token string">'E40FD7D49B8B7EF46F47407D583C3538'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 17030921</span>

<span class="token keyword">create</span> <span class="token keyword">table</span> t_memorials
<span class="token punctuation">(</span>
    memorials_id          <span class="token keyword">int</span> <span class="token keyword">primary</span> <span class="token keyword">key</span> <span class="token keyword">auto_increment</span><span class="token punctuation">,</span>
    memorials_title       <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span>     <span class="token operator">not</span> <span class="token boolean">null</span><span class="token punctuation">,</span>
    memorials_content     <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span> <span class="token operator">not</span> <span class="token boolean">null</span><span class="token punctuation">,</span>
    memorials_emp         <span class="token keyword">int</span>           <span class="token operator">not</span> <span class="token boolean">null</span><span class="token punctuation">,</span>
    memorials_create_time <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    feedback_time       <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    feedback_content    <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    memorials_status      <span class="token keyword">int</span>           <span class="token operator">not</span> <span class="token boolean">null</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">insert</span> <span class="token keyword">into</span> t_memorials<span class="token punctuation">(</span>memorials_title<span class="token punctuation">,</span>
                      memorials_content<span class="token punctuation">,</span>
                      memorials_emp<span class="token punctuation">,</span>
                      memorials_create_time<span class="token punctuation">,</span>
                      feedback_time<span class="token punctuation">,</span>
                      feedback_content<span class="token punctuation">,</span>
                      memorials_status<span class="token punctuation">)</span>
<span class="token keyword">values</span> <span class="token punctuation">(</span><span class="token string">'浙江巡抚奏钱塘堤决口疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊，不好啦！钱塘江发大水啦！堤坝冲毁啦！您看这咋弄啊！'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1690-05-07'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'左都御史参鳌拜圈地疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊，鳌拜这厮不是东西呀！占老百姓的地哇！还打人呀！您看咋弄啊！'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'1690-04-14'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'都察院劾吴三桂不臣疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊，不得了啦！吴三桂那孙子想造反呀！'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1693-11-18'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'兵部奏准噶尔犯境疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊，不得了啦！葛尔丹要打过来了呀！'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'1693-11-18'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'朝鲜使臣朝拜事宜呈皇上御览'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！朝鲜国的人要来啦！咱们请他们吃猪肉炖粉条子吧！'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1680-06-11'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'英吉利炮舰购买事宜疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！英国的小船船咱们买多少啊？'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'1680-06-12'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'劾杭州织造贪墨疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！杭州织造有问题啊！'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1680-06-13'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'禀畅春园落成疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！畅春园修好了哇！您啥时候过来看看呀！'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'1680-06-14'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'请旨木兰秋狝疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！秋天到啦，又该打猎啦！'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1680-06-15'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'核准西北军饷银两疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！您看看这钱数算的对不对呀！'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'1680-06-16'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'请旨裁撤三藩疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！咱们不裁撤三藩就芭比Q了哇！'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1680-06-17'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'蒙古王公进京朝拜疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！蒙古王公要来啦！咱们请他们吃猪肉炖粉条子吧！'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'1680-06-18'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'礼部请旨九阿哥赐名疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！您看九阿哥该叫什么名字呀？'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1680-06-19'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'户部尚书请旨告老还乡疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！臣想回家养老啦！您看看啥时候给臣把俸禄结一下啊！'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'1680-06-20'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">(</span><span class="token string">'查江宁织造贪墨疏'</span><span class="token punctuation">,</span> <span class="token string">'皇上啊！江宁织造有问题啊！'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'1680-06-21'</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token boolean">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
       <span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br></div></div><h3 id="_2逻辑建模"><a href="#_2逻辑建模" class="header-anchor">#</a> ②逻辑建模</h3> <h4 id="_1-emp-实体类"><a href="#_1-emp-实体类" class="header-anchor">#</a> [1] Emp 实体类</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Emp</span> <span class="token punctuation">{</span>

    <span class="token keyword">private</span> <span class="token class-name">Integer</span> empId<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> empName<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> empPosition<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> loginAccount<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> loginPassword<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="_2-memorials-实体类"><a href="#_2-memorials-实体类" class="header-anchor">#</a> [2] Memorials 实体类</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Memorials</span> <span class="token punctuation">{</span>

    <span class="token keyword">private</span> <span class="token class-name">Integer</span> memorialsId<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> memorialsTitle<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> memorialsContent<span class="token punctuation">;</span>
    
    <span class="token comment">// 奏折摘要数据库没有，这里是为了配和页面显示</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> memorialsContentDigest<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> memorialsEmp<span class="token punctuation">;</span>

    <span class="token comment">// 员工姓名数据库没有，这里是为了配合页面显示</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> memorialsEmpName<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> memorialsCreateTime<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> feedbackTime<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> feedbackContent<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> memorialsStatus<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="_2、数据库连接信息"><a href="#_2、数据库连接信息" class="header-anchor">#</a> 2、数据库连接信息</h2> <p>说明：这是我们第一次用到 Maven 约定目录结构中的 resources 目录，这个目录存放各种配置文件。</p> <p><img src="" alt="images"></p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token attr-name">driverClassName</span><span class="token punctuation">=</span><span class="token attr-value">com.mysql.jdbc.Driver</span>
<span class="token attr-name">url</span><span class="token punctuation">=</span><span class="token attr-value">jdbc:mysql://192.168.198.100:3306/db_imperial_court</span>
<span class="token attr-name">username</span><span class="token punctuation">=</span><span class="token attr-value">root</span>
<span class="token attr-name">password</span><span class="token punctuation">=</span><span class="token attr-value">atguigu</span>
<span class="token attr-name">initialSize</span><span class="token punctuation">=</span><span class="token attr-value">10</span>
<span class="token attr-name">maxActive</span><span class="token punctuation">=</span><span class="token attr-value">20</span>
<span class="token attr-name">maxWait</span><span class="token punctuation">=</span><span class="token attr-value">10000</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="_3、获取数据库连接"><a href="#_3、获取数据库连接" class="header-anchor">#</a> 3、获取数据库连接</h2> <h3 id="_1创建-jdbcutils-工具类"><a href="#_1创建-jdbcutils-工具类" class="header-anchor">#</a> ①创建 JDBCUtils 工具类</h3> <p><img src="" alt="images"></p> <h3 id="_2创建-javax-sql-datasource-对象"><a href="#_2创建-javax-sql-datasource-对象" class="header-anchor">#</a> ②创建 javax.sql.DataSource 对象</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 将数据源对象设置为静态属性，保证大对象的单一实例</span>
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">DataSource</span> dataSource<span class="token punctuation">;</span>

<span class="token keyword">static</span> <span class="token punctuation">{</span>

    <span class="token comment">// 1.创建一个用于存储外部属性文件信息的Properties对象</span>
    <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 2.使用当前类的类加载器加载外部属性文件：jdbc.properties</span>
    <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getClassLoader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span><span class="token string">&quot;jdbc.properties&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">try</span> <span class="token punctuation">{</span>

        <span class="token comment">// 3.将外部属性文件jdbc.properties中的数据加载到properties对象中</span>
        properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 4.创建数据源对象</span>
        dataSource <span class="token operator">=</span> <span class="token class-name">DruidDataSourceFactory</span><span class="token punctuation">.</span><span class="token function">createDataSource</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="_3创建-threadlocal-对象"><a href="#_3创建-threadlocal-对象" class="header-anchor">#</a> ③创建 ThreadLocal 对象</h3> <h4 id="_1-提出需求"><a href="#_1-提出需求" class="header-anchor">#</a> [1]提出需求</h4> <h5 id="_1-在一个方法内控制事务"><a href="#_1-在一个方法内控制事务" class="header-anchor">#</a> (1)在一个方法内控制事务</h5> <p>如果在每一个 Service 方法中都写下面代码，那么代码重复性就太高了：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">try</span><span class="token punctuation">{</span>

	<span class="token comment">// 1、获取数据库连接</span>
	<span class="token comment">// 重要：要保证参与事务的多个数据库操作（SQL 语句）使用的是同一个数据库连接</span>
	<span class="token class-name">Connection</span> conn <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token comment">// 2、核心操作</span>
	<span class="token comment">// ...</span>
	
	<span class="token comment">// 3、核心操作成功结束，可以提交事务</span>
	conn<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>

	<span class="token comment">// 4、核心操作抛出异常，必须回滚事务</span>
	conn<span class="token punctuation">.</span><span class="token function">rollBack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span><span class="token keyword">finally</span><span class="token punctuation">{</span>

	<span class="token comment">// 5、释放数据库连接</span>
	<span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">releaseConnection</span><span class="token punctuation">(</span>conn<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h5 id="_2-将重复代码抽取到-filter"><a href="#_2-将重复代码抽取到-filter" class="header-anchor">#</a> (2)将重复代码抽取到 Filter</h5> <p>所谓『当前请求覆盖的 Servlet 方法、Service 方法、Dao 方法』其实就是 chain.doFilter(request, response) <span style="color:blue;font-weight:bold;">间接调用</span>的方法。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doFilter</span><span class="token punctuation">(</span><span class="token class-name">ServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">ServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">FilterChain</span> chain<span class="token punctuation">)</span><span class="token punctuation">{</span>

	<span class="token keyword">try</span><span class="token punctuation">{</span>

		<span class="token comment">// 1、获取数据库连接</span>
		<span class="token comment">// 重要：要保证参与事务的多个数据库操作（SQL 语句）使用的是同一个数据库连接</span>
		<span class="token class-name">Connection</span> conn <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">// 重要操作：关闭自动提交功能</span>
        connection<span class="token punctuation">.</span><span class="token function">setAutoCommit</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		
		<span class="token comment">// 2、核心操作：通过 chain 对象放行当前请求</span>
		<span class="token comment">// 这样就可以保证当前请求覆盖的 Servlet 方法、Service 方法、Dao 方法都在同一个事务中。</span>
		<span class="token comment">// 同时各个请求都经过这个 Filter，所以当前事务控制的代码在这里只写一遍就行了，</span>
		<span class="token comment">// 避免了代码的冗余。</span>
		chain<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
		<span class="token comment">// 3、核心操作成功结束，可以提交事务</span>
		conn<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token punctuation">}</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>

		<span class="token comment">// 4、核心操作抛出异常，必须回滚事务</span>
		conn<span class="token punctuation">.</span><span class="token function">rollBack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token punctuation">}</span><span class="token keyword">finally</span><span class="token punctuation">{</span>

		<span class="token comment">// 5、释放数据库连接</span>
		<span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">releaseConnection</span><span class="token punctuation">(</span>conn<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
	<span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h5 id="_3-数据的跨方法传递"><a href="#_3-数据的跨方法传递" class="header-anchor">#</a> (3)数据的跨方法传递</h5> <p>通过 JDBCUtils 工具类获取到的 Connection 对象需要传递给 Dao 方法，让事务涉及到的所有 Dao 方法用的都是同一个 Connection 对象。</p> <p>但是 Connection 对象无法通过 chain.doFilter() 方法以参数的形式传递过去。</p> <p>所以从获取到 Connection 对象到使用 Connection 对象中间隔着很多不是我们自己声明的方法——我们无法决定它们的参数。</p> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img011.ee4b484a.png" alt="images"></p> <h4 id="_2-threadlocal-对象的功能"><a href="#_2-threadlocal-对象的功能" class="header-anchor">#</a> [2] ThreadLocal 对象的功能</h4> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img012.7aef6f35.png" alt="images"></p> <ul><li>全类名：java.lang.ThreadLocal&lt;T&gt;</li> <li>泛型 T：要绑定到当前线程的数据的类型</li> <li>具体三个主要的方法：</li></ul> <table><thead><tr><th>方法名</th> <th>功能</th></tr></thead> <tbody><tr><td>set(T value)</td> <td>将数据绑定到当前线程</td></tr> <tr><td>get()</td> <td>从当前线程获取已绑定的数据</td></tr> <tr><td>remove()</td> <td>将数据从当前线程移除</td></tr></tbody></table> <h4 id="_3-java-代码"><a href="#_3-java-代码" class="header-anchor">#</a> [3] Java 代码</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 由于 ThreadLocal 对象需要作为绑定数据时 k-v 对中的 key，所以要保证唯一性</span>
<span class="token comment">// 加 static 声明为静态资源即可保证唯一性</span>
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">ThreadLocal</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Connection</span><span class="token punctuation">&gt;</span></span> threadLocal <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadLocal</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_4声明方法-获取数据库连接"><a href="#_4声明方法-获取数据库连接" class="header-anchor">#</a> ④声明方法：获取数据库连接</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 工具方法：获取数据库连接并返回
 * @return
 */</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Connection</span> <span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// 1、尝试从当前线程检查是否存在已经绑定的 Connection 对象</span>
        connection <span class="token operator">=</span> threadLocal<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 2、检查 Connection 对象是否为 null</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>connection <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            
            <span class="token comment">// 3、如果为 null，则从数据源获取数据库连接</span>
            connection <span class="token operator">=</span> dataSource<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">// 4、获取到数据库连接后绑定到当前线程</span>
            threadLocal<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
            
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">// 为了调用工具方法方便，编译时异常不往外抛</span>
        <span class="token comment">// 为了不掩盖问题，捕获到的编译时异常封装为运行时异常抛出</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> connection<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><h3 id="_5声明方法-释放数据库连接"><a href="#_5声明方法-释放数据库连接" class="header-anchor">#</a> ⑤声明方法：释放数据库连接</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 释放数据库连接
 */</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">releaseConnection</span><span class="token punctuation">(</span><span class="token class-name">Connection</span> connection<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>connection <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// 在数据库连接池中将当前连接对象标记为空闲</span>
            connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">// 将当前数据库连接从当前线程上移除</span>
            threadLocal<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_6初步测试"><a href="#_6初步测试" class="header-anchor">#</a> ⑥初步测试</h3> <p><img src="" alt="images"></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ImperialCourtTest</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testGetConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;connection = &quot;</span> <span class="token operator">+</span> connection<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">releaseConnection</span><span class="token punctuation">(</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_7完整代码"><a href="#_7完整代码" class="header-anchor">#</a> ⑦完整代码</h3> <p><a href="https://heavy_code_industry.gitee.io/code_heavy_industry/pro002-maven/chapter06/jdbc-utils.html">传送门</a></p> <h2 id="_4、basedao"><a href="#_4、basedao" class="header-anchor">#</a> 4、BaseDao</h2> <p><img src="" alt="images"></p> <h3 id="_1泛型的说明"><a href="#_1泛型的说明" class="header-anchor">#</a> ①泛型的说明</h3> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img015.685c2745.png" alt="images"></p> <h3 id="_2创建-queryrunner-对象"><a href="#_2创建-queryrunner-对象" class="header-anchor">#</a> ②创建 QueryRunner 对象</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code>    <span class="token comment">// DBUtils 工具包提供的数据库操作对象</span>
    <span class="token keyword">private</span> <span class="token class-name">QueryRunner</span> runner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryRunner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_3通用增删改方法"><a href="#_3通用增删改方法" class="header-anchor">#</a> ③通用增删改方法</h3> <p><span style="color:red;font-weight:bold;">特别说明</span>：在 BaseDao 方法中获取数据库连接但是不做释放，因为我们要在控制事务的 Filter 中统一释放。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 通用的增删改方法，insert、delete、update 操作都可以用这个方法
 * @param sql 执行操作的 SQL 语句
 * @param parameters SQL 语句的参数
 * @return 受影响的行数
 */</span>
<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">String</span> sql<span class="token punctuation">,</span> <span class="token class-name">Object</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> parameters<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">int</span> affectedRowNumbers <span class="token operator">=</span> runner<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> sql<span class="token punctuation">,</span> parameters<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token keyword">return</span> affectedRowNumbers<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 如果真的抛出异常，则将编译时异常封装为运行时异常抛出</span>
        <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="_4查询单个对象"><a href="#_4查询单个对象" class="header-anchor">#</a> ④查询单个对象</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 查询单个对象
 * @param sql 执行查询的 SQL 语句
 * @param entityClass 实体类对应的 Class 对象
 * @param parameters 传给 SQL 语句的参数
 * @return 查询到的实体类对象
 */</span>
<span class="token keyword">public</span> <span class="token class-name">T</span> <span class="token function">getSingleBean</span><span class="token punctuation">(</span><span class="token class-name">String</span> sql<span class="token punctuation">,</span> <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> entityClass<span class="token punctuation">,</span> <span class="token class-name">Object</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> parameters<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// 获取数据库连接</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> runner<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> sql<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">BeanHandler</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span>entityClass<span class="token punctuation">)</span><span class="token punctuation">,</span> parameters<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 如果真的抛出异常，则将编译时异常封装为运行时异常抛出</span>
        <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="_5查询多个对象"><a href="#_5查询多个对象" class="header-anchor">#</a> ⑤查询多个对象</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 查询返回多个对象的方法
 * @param sql 执行查询操作的 SQL 语句
 * @param entityClass 实体类的 Class 对象
 * @param parameters SQL 语句的参数
 * @return 查询结果
 */</span>
<span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token function">getBeanList</span><span class="token punctuation">(</span><span class="token class-name">String</span> sql<span class="token punctuation">,</span> <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> entityClass<span class="token punctuation">,</span> <span class="token class-name">Object</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> parameters<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token comment">// 获取数据库连接</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> runner<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> sql<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">BeanListHandler</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span>entityClass<span class="token punctuation">)</span><span class="token punctuation">,</span> parameters<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 如果真的抛出异常，则将编译时异常封装为运行时异常抛出</span>
        <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="_6测试"><a href="#_6测试" class="header-anchor">#</a> ⑥测试</h3> <p><img src="" alt="images"></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">private</span> <span class="token class-name">BaseDao</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Emp</span><span class="token punctuation">&gt;</span></span> baseDao <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BaseDao</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testGetSingleBean</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">&quot;select emp_id empId,emp_name empName,emp_position empPosition,login_account loginAccount,login_password loginPassword from t_emp where emp_id=?&quot;</span><span class="token punctuation">;</span>

    <span class="token class-name">Emp</span> emp <span class="token operator">=</span> baseDao<span class="token punctuation">.</span><span class="token function">getSingleBean</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token class-name">Emp</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;emp = &quot;</span> <span class="token operator">+</span> emp<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testGetBeanList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">&quot;select emp_id empId,emp_name empName,emp_position empPosition,login_account loginAccount,login_password loginPassword from t_emp&quot;</span><span class="token punctuation">;</span>

    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Emp</span><span class="token punctuation">&gt;</span></span> empList <span class="token operator">=</span> baseDao<span class="token punctuation">.</span><span class="token function">getBeanList</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token class-name">Emp</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Emp</span> emp <span class="token operator">:</span> empList<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;emp = &quot;</span> <span class="token operator">+</span> emp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">&quot;update t_emp set emp_position=? where emp_id=?&quot;</span><span class="token punctuation">;</span>

    <span class="token class-name">String</span> empPosition <span class="token operator">=</span> <span class="token string">&quot;minister&quot;</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> empId <span class="token operator">=</span> <span class="token string">&quot;3&quot;</span><span class="token punctuation">;</span>

    <span class="token keyword">int</span> affectedRowNumber <span class="token operator">=</span> baseDao<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> empPosition<span class="token punctuation">,</span> empId<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;affectedRowNumber = &quot;</span> <span class="token operator">+</span> affectedRowNumber<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><h2 id="_5、子类-dao"><a href="#_5、子类-dao" class="header-anchor">#</a> 5、子类 Dao</h2> <p>创建接口和实现类如下：</p> <p><img src="" alt="images"></p> <p><a href="verse01.html">上一节</a> <a href="index.html">回目录</a> <a href="verse03.html">下一节</a></p></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="../../assets/js/app.972bd0f0.js" defer></script><script src="../../assets/js/4.904e76b5.js" defer></script><script src="../../assets/js/53.eae65ff7.js" defer></script>
  </body>

<!-- Mirrored from heavy_code_industry.gitee.io/code_heavy_industry/pro002-maven/chapter06/verse02.html by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 10 May 2022 05:10:02 GMT -->
</html>
